﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;

namespace COER
{
    public partial class FormLaserBlockSel : Form
    {
        public FormLaserBlockSel()
        {
            InitializeComponent();

            GetControls(this.Controls);
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((DLLPC.PCAP.PCButton)item).OP = Kernel.OP;
                }

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        private uint selNo = 0;
        public void ShowForm(uint sels)
        {
            selNo = sels;
            InitStatus();
            this.timer1.Start();
            this.ShowDialog();
        }

        private bool sel_00 = false;
        private bool sel_01 = false;
        private bool sel_02 = false;
        private bool sel_03 = false;
        private bool sel_04 = false;
        private bool sel_05 = false;
        private bool sel_06 = false;
        private bool sel_07 = false;
        private bool sel_08 = false;
        private bool sel_09 = false;
        private bool sel_10 = false;
        private bool sel_11 = false;
        private bool sel_12 = false;
        private bool sel_13 = false;
        private bool sel_14 = false;
        private bool sel_15 = false;
        private bool sel_16 = false;
        private bool sel_17 = false;
        private bool sel_18 = false;
        private bool sel_19 = false;
        private bool sel_20 = false;
        private bool sel_21 = false;
        private bool sel_22 = false;
        private bool sel_23 = false;
        private bool sel_24 = false;
        private bool sel_25 = false;
        private bool sel_26 = false;
        private bool sel_27 = false;
        private bool sel_28 = false;
        private bool sel_29 = false;
        private bool sel_30 = false;
        private bool sel_31 = false;
        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                case "Save":
                    {
                        for (int i = 0; i < 32; i++)
                        {
                            bool bln = MemoryAccess.GetBit(selNo, i);
                            if (bln)
                            {
                                BlockInfo block = new BlockInfo();
                                block.Idx = i;
                                if (ParameterHelper.CurParam.LaserParam.Blocks.FindIndex(a => a.Idx == i) < 0)
                                {
                                    ParameterHelper.CurParam.LaserParam.Blocks.Add(block);
                                }
                            }
                            else
                            {
                                int temp = ParameterHelper.CurParam.LaserParam.Blocks.FindIndex(a => a.Idx == i);
                                if (temp >= 0)
                                {
                                    ParameterHelper.CurParam.LaserParam.Blocks.RemoveAt(temp);
                                }
                            }
                        }

                        ParameterHelper.CurParam.LaserParam.LaserBlockSel = selNo;
                        ProcedureHelper_EQ.SaveRecParam();
                        ProcedureHelper_EQ.RecGridRefresh = true;
                        this.timer1.Stop();
                        this.Visible = false;
                    }
                    break;
                case "Cancel":
                    {
                        this.timer1.Stop();
                        this.Visible = false;
                    }
                    break;
                case "Sel_00":
                    {
                        sel_00 = !sel_00;
                        MemoryAccess.SetBit(ref selNo, sel_00, 0);
                    }
                    break;
                case "Sel_01":
                    {
                        sel_01 = !sel_01;
                        MemoryAccess.SetBit(ref selNo, sel_01, 1);
                    }
                    break;
                case "Sel_02":
                    {
                        sel_02 = !sel_02;
                        MemoryAccess.SetBit(ref selNo, sel_02, 2);
                    }
                    break;
                case "Sel_03":
                    {
                        sel_03 = !sel_03;
                        MemoryAccess.SetBit(ref selNo, sel_03, 3);
                    }
                    break;
                case "Sel_04":
                    {
                        sel_04 = !sel_04;
                        MemoryAccess.SetBit(ref selNo, sel_04, 4);
                    }
                    break;
                case "Sel_05":
                    {
                        sel_05 = !sel_05;
                        MemoryAccess.SetBit(ref selNo, sel_05, 5);
                    }
                    break;
                case "Sel_06":
                    {
                        sel_06 = !sel_06;
                        MemoryAccess.SetBit(ref selNo, sel_06, 6);
                    }
                    break;
                case "Sel_07":
                    {
                        sel_07 = !sel_07;
                        MemoryAccess.SetBit(ref selNo, sel_07, 7);
                    }
                    break;
                case "Sel_08":
                    {
                        sel_08 = !sel_08;
                        MemoryAccess.SetBit(ref selNo, sel_08, 8);
                    }
                    break;
                case "Sel_09":
                    {
                        sel_09 = !sel_09;
                        MemoryAccess.SetBit(ref selNo, sel_09, 9);
                    }
                    break;
                case "Sel_10":
                    {
                        sel_10 = !sel_10;
                        MemoryAccess.SetBit(ref selNo, sel_10, 10);
                    }
                    break;
                case "Sel_11":
                    {
                        sel_11 = !sel_11;
                        MemoryAccess.SetBit(ref selNo, sel_11, 11);
                    }
                    break;
                case "Sel_12":
                    {
                        sel_12 = !sel_12;
                        MemoryAccess.SetBit(ref selNo, sel_12, 12);
                    }
                    break;
                case "Sel_13":
                    {
                        sel_13 = !sel_13;
                        MemoryAccess.SetBit(ref selNo, sel_13, 13);
                    }
                    break;
                case "Sel_14":
                    {
                        sel_14 = !sel_14;
                        MemoryAccess.SetBit(ref selNo, sel_14, 14);
                    }
                    break;
                case "Sel_15":
                    {
                        sel_15 = !sel_15;
                        MemoryAccess.SetBit(ref selNo, sel_15, 15);
                    }
                    break;
                case "Sel_16":
                    {
                        sel_16 = !sel_16;
                        MemoryAccess.SetBit(ref selNo, sel_16, 16);
                    }
                    break;
                case "Sel_17":
                    {
                        sel_17 = !sel_17;
                        MemoryAccess.SetBit(ref selNo, sel_17, 17);
                    }
                    break;
                case "Sel_18":
                    {
                        sel_18 = !sel_18;
                        MemoryAccess.SetBit(ref selNo, sel_18, 18);
                    }
                    break;
                case "Sel_19":
                    {
                        sel_19 = !sel_19;
                        MemoryAccess.SetBit(ref selNo, sel_19, 19);
                    }
                    break;
                case "Sel_20":
                    {
                        sel_20 = !sel_20;
                        MemoryAccess.SetBit(ref selNo, sel_20, 20);
                    }
                    break;
                case "Sel_21":
                    {
                        sel_21 = !sel_21;
                        MemoryAccess.SetBit(ref selNo, sel_21, 21);
                    }
                    break;
                case "Sel_22":
                    {
                        sel_22 = !sel_22;
                        MemoryAccess.SetBit(ref selNo, sel_22, 22);
                    }
                    break;
                case "Sel_23":
                    {
                        sel_23 = !sel_23;
                        MemoryAccess.SetBit(ref selNo, sel_23, 23);
                    }
                    break;
                case "Sel_24":
                    {
                        sel_24 = !sel_24;
                        MemoryAccess.SetBit(ref selNo, sel_24, 24);
                    }
                    break;
                case "Sel_25":
                    {
                        sel_25 = !sel_25;
                        MemoryAccess.SetBit(ref selNo, sel_25, 25);
                    }
                    break;
                case "Sel_26":
                    {
                        sel_26 = !sel_26;
                        MemoryAccess.SetBit(ref selNo, sel_26, 26);
                    }
                    break;
                case "Sel_27":
                    {
                        sel_27 = !sel_27;
                        MemoryAccess.SetBit(ref selNo, sel_27, 27);
                    }
                    break;
                case "Sel_28":
                    {
                        sel_28 = !sel_28;
                        MemoryAccess.SetBit(ref selNo, sel_28, 28);
                    }
                    break;
                case "Sel_29":
                    {
                        sel_29 = !sel_29;
                        MemoryAccess.SetBit(ref selNo, sel_29, 29);
                    }
                    break;
                case "Sel_30":
                    {
                        sel_30 = !sel_30;
                        MemoryAccess.SetBit(ref selNo, sel_30, 30);
                    }
                    break;
                case "Sel_31":
                    {
                        sel_31 = !sel_31;
                        MemoryAccess.SetBit(ref selNo, sel_31, 31);
                    }
                    break;
            }
        }

        private void InitStatus()
        {
            sel_00 = MemoryAccess.GetBit(selNo, 0);
            sel_01 = MemoryAccess.GetBit(selNo, 1);
            sel_02 = MemoryAccess.GetBit(selNo, 2);
            sel_03 = MemoryAccess.GetBit(selNo, 3);
            sel_04 = MemoryAccess.GetBit(selNo, 4);
            sel_05 = MemoryAccess.GetBit(selNo, 5);
            sel_06 = MemoryAccess.GetBit(selNo, 6);
            sel_07 = MemoryAccess.GetBit(selNo, 7);
            sel_08 = MemoryAccess.GetBit(selNo, 8);
            sel_09 = MemoryAccess.GetBit(selNo, 9);
            sel_10 = MemoryAccess.GetBit(selNo, 10);
            sel_11 = MemoryAccess.GetBit(selNo, 11);
            sel_12 = MemoryAccess.GetBit(selNo, 12);
            sel_13 = MemoryAccess.GetBit(selNo, 13);
            sel_14 = MemoryAccess.GetBit(selNo, 14);
            sel_15 = MemoryAccess.GetBit(selNo, 15);
            sel_16 = MemoryAccess.GetBit(selNo, 16);
            sel_17 = MemoryAccess.GetBit(selNo, 17);
            sel_18 = MemoryAccess.GetBit(selNo, 18);
            sel_19 = MemoryAccess.GetBit(selNo, 19);
            sel_20 = MemoryAccess.GetBit(selNo, 20);
            sel_21 = MemoryAccess.GetBit(selNo, 21);
            sel_22 = MemoryAccess.GetBit(selNo, 22);
            sel_23 = MemoryAccess.GetBit(selNo, 23);
            sel_24 = MemoryAccess.GetBit(selNo, 24);
            sel_25 = MemoryAccess.GetBit(selNo, 25);
            sel_26 = MemoryAccess.GetBit(selNo, 26);
            sel_27 = MemoryAccess.GetBit(selNo, 27);
            sel_28 = MemoryAccess.GetBit(selNo, 28);
            sel_29 = MemoryAccess.GetBit(selNo, 29);
            sel_30 = MemoryAccess.GetBit(selNo, 30);
            sel_31 = MemoryAccess.GetBit(selNo, 31);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            btnSel_00.Monitor(sel_00);
            btnSel_01.Monitor(sel_01);
            btnSel_02.Monitor(sel_02);
            btnSel_03.Monitor(sel_03);
            btnSel_04.Monitor(sel_04);
            btnSel_05.Monitor(sel_05);
            btnSel_06.Monitor(sel_06);
            btnSel_07.Monitor(sel_07);
            btnSel_08.Monitor(sel_08);
            btnSel_09.Monitor(sel_09);
            btnSel_10.Monitor(sel_10);
            btnSel_11.Monitor(sel_11);
            btnSel_12.Monitor(sel_12);
            btnSel_13.Monitor(sel_13);
            btnSel_14.Monitor(sel_14);
            btnSel_15.Monitor(sel_15);
            btnSel_16.Monitor(sel_16);
            btnSel_17.Monitor(sel_17);
            btnSel_18.Monitor(sel_18);
            btnSel_19.Monitor(sel_19);
            btnSel_20.Monitor(sel_20);
            btnSel_21.Monitor(sel_21);
            btnSel_22.Monitor(sel_22);
            btnSel_23.Monitor(sel_23);
            btnSel_24.Monitor(sel_24);
            btnSel_25.Monitor(sel_25);
            btnSel_26.Monitor(sel_26);
            btnSel_27.Monitor(sel_27);
            btnSel_28.Monitor(sel_28);
            btnSel_29.Monitor(sel_29);
            btnSel_30.Monitor(sel_30);
            btnSel_31.Monitor(sel_31);
        }

        private void btnSel_00_Click(object sender, EventArgs e)
        {

        }
    }
}
